import router from './router'
import store from './store'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式
import { secondDirectory, tabList } from '@/utils/config.js'
import { getToken } from '@/utils/auth' // 验权
import {
	Message
} from 'element-ui'
import Layout from './views/layout/Layout'

const whiteList = ['/login'] // 不重定向白名单
router.beforeEach((to, from, next) => {
	console.log(to)
	NProgress.start()
	// 刷新判断当前是否登录状态
	let loginName = window.localStorage.getItem('admin') ? JSON.parse(window.localStorage.getItem('admin')) : ''
	let menuList = sessionStorage.getItem('menuList') ? JSON.parse(sessionStorage.getItem('menuList')) : []
	let adminList = sessionStorage.getItem('adminList') ? JSON.parse(sessionStorage.getItem('adminList')) : []
	console.log(adminList)
	if(getToken('mobile') != 'undefined' && getToken()) {
		// 如果当前是登录路由 跳转首页
		let neStep = ''
		if(menuList && menuList.length > 0) {
			neStep = menuList[0].menuUrl
		}
		to.path === '/login' ? next({
			path: neStep
		}) : next()
		NProgress.done()
		store.dispatch('SET_TAB_CURRENT_INDEX', 1)
		// 记录当前路由对象的 二级分类 和tabs显示
		
		let list = []
		adminList.forEach((item) => {
			if(to.name) {
				if(item.menuUrl == to.name.toLowerCase()) {
					list = item
				}
			}
			
		})
		store.dispatch('SET_SECOND_DIRECTORY', list)
		
		store.dispatch('SET_TAB_LIST', tabList[to.name])

	} else {
		if(whiteList.indexOf(to.path) !== -1) {
			next()
		} else {
			next('/login')
			NProgress.done()
		}
	}
})

router.afterEach(() => {
	NProgress.done() // 结束Progress
})